<%
# Copyright (C) 2011 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
%>

<%
  rubric_criterion ||= nil
  criterion = rubric_criterion || nil
  assessing ||= false
  rubric ||= nil
  rubric_association ||= nil
  edit_view ||= false
  assessment_rating ||= nil
  range_rating_enabled = @domain_root_account.feature_enabled?(:rubric_criterion_range)
  range_rating_visible = range_rating_enabled && !criterion.try(:learning_outcome_id)

  learning_outcome_criterion = criterion.try(:learning_outcome_id)
  description = criterion.try(:description) || t('defaults.description', "Description of criterion")
%>
<tr id="criterion_<%= criterion ? criterion.id : "blank" %>" class="criterion <%= "blank" unless criterion %> <%= 'ignore_criterion_for_scoring' if criterion && criterion.ignore_for_scoring %> <%= 'learning_outcome_criterion' if learning_outcome_criterion %>" style="<%= hidden unless criterion %>">
  <td class="criterion_description hover-container pad-box-micro">
    <div class="container">
      <div class="links editing">
        <% if !learning_outcome_criterion %>
          <a href="#" class="edit_criterion_link"><i class="icon-edit standalone-icon"></i></a>
        <% end %>
        <a href="#" class="delete_criterion_link"><i class="icon-trash standalone-icon"></i></a>
      </div>
      <div class="description_content">
        <i class="learning_outcome_flag icon-outcomes" aria-hidden="true"></i>
        
        <span class="description criterion_description_value"><%= description %></span>
        <span class="learning_outcome_id" style="display: none;"><%= criterion.try(:learning_outcome_id) %></span>
        <span class="criterion_id" style="display: none;"><%= criterion.try(:id) %></span>
        <div class="long_description_holder editing <%= 'empty' if !criterion || criterion.long_description.blank? %>">
          <a href="#" class="long_description_link"><%= t 'links.view_longer_description', "view longer description" %></a>
          <textarea class="long_description" aria-label="<%= t 'labels.long_description', "Long Description" %>" style="display: none;"><%= h((criterion.try(:long_description) || '')) %></textarea>
        </div>
        <div class="hide_when_learning_outcome <%= range_rating_visible ? '' : 'hidden' %>">
          <div class="criterion_use_range_div editing">
            <label><%= t 'Range' %>
              <input type="checkbox" class="criterion_use_range" <%= 'checked' if (criterion.try(:criterion_use_range) && range_rating_enabled) %>/></label>
          </div>
        </div>
        <div class="threshold">
          <%= before_label :threshold, "threshold" %>
          <%= t :points, "%{points} pts", :points => %{<span class="mastery_points">#{n((criterion.mastery_points rescue 5))}</span>}.html_safe %>
        </div>
      </div>

    </div>
    <% if rubric && !rubric.free_form_criterion_comments && assessment_rating && assessment_rating[:comments] %>
      <div class="assessment-comments">
        <h6>{{#t}}Instructor Comments{{/t}}</h6>
        <div><%= format_message( assessment_rating[:comments] ).first %></div>
      </div>
    <% end %>
  </td>
  <td style="padding: 0;">
    <% if !edit_view && criterion && rubric && rubric.free_form_criterion_comments %>
      <div class="custom_ratings_entry assessing">
        <div style="margin-left: 5px; margin-top: 2px; <%= hidden unless rubric_association && rubric_association.summary_data && rubric_association.summary_data[:saved_comments] && rubric_association.summary_data[:saved_comments][criterion.id.to_s] %>" class="saved_custom_rating_holder">
          <select class="saved_custom_rating">
            <option value=""><%= t 'options.select', "[ Select ]" %></option>
            <% (rubric_association.summary_data[:saved_comments][criterion.id.to_s] || [] rescue []).each do |comment| %>
              <option value="<%= URI.escape(comment) %>" class="comment"><%= comment[0..50] %></option>
            <% end %>
          </select>
        </div>
        <textarea style="margin: 5px; min-width: 100px; height: 35px;" class="custom_rating_field"></textarea>
        <div>
          <input type="checkbox" id="save_criterion_message_<%= criterion ? criterion.id : "blank" %>" class="save_custom_rating" value="1"/>
          <label for="save_criterion_message_<%= criterion ? criterion.id : "blank" %>"><%= t 'labels.save_criterion_for_reuse', "Save this comment for reuse" %></label>
        </div>
      </div>
      <div class="displaying custom_rating_comments not_assessing" style="font-size: 0.8em; padding: 0px 5px;">
        <%= assessment_rating && (assessment_rating[:comments_html] ? assessment_rating[:comments_html].html_safe : assessment_rating[:comments]) %>
      </div>
    <% else %>
      <table class="ratings" style="<%= hidden if rubric && rubric.free_form_criterion_comments %>"><tr>
        <% ratings = (criterion.ratings rescue nil); ratings = nil if ratings && ratings.length < 2  %>
        <% ratings ||= [OpenObject.new(:id => "blank", :description => t(:full_marks, "Full Marks"), :points => 5), OpenObject.new(:id => "blank_2", :description => t(:no_marks, "No Marks"), :points => 0)] %>
        <% ratings.each_index do |idx| %>
          <% rating = ratings[idx];
             rating.edge = (idx == 0 || idx == ratings.length - 1);
             next_rating = ratings[idx+1] if idx != ratings.length - 1;
             rating_min = (next_rating ? next_rating.points : 0);
             use_range = (criterion.try(:criterion_use_range) && rating_min.try(:to_f) != rating.points.try(:to_f) && range_rating_enabled);
          %>
          <td id="rating_<%= rating.id %>" class="rating <%= "edge_rating" if rating.edge %> <%= "infinitesimal" if rating_min.try(:to_f) == rating.points.try(:to_f) %>">
          <div class="container">
            <% if !learning_outcome_criterion %>
              <div class="editing links">
                <a href="#" class="edit_rating_link"><i class="icon-edit standalone-icon"></i></a>
                <a href="#" class="delete_rating_link"><i class="icon-trash standalone-icon" ></i></a>
              </div>
              <div class="clear"></div>
            <% end %>
            <span class="nobr">
              <span class="points"><%= n(rating.points) %></span>
              <span class="range_rating" style="<%= hidden if !use_range %>">{{#t}}to') %> ><span class="min_points"><%= n(rating_min) %></span></span> <%= t('pts{{/t}}
            </span>
            <div class="description rating_description_value"><%= rating.description %></div>
            <div class="rating_long_description small_description"><%= rating.long_description %></div>
            <span class="rating_id" style="display: none;"><%= rating && rating.id %></span>
            <% if !learning_outcome_criterion %>
              <div class="editing links add_rating_link">
                <a href="#" class="add_rating_link_after" aria-label="<%= t 'Add rating' %>"><i class="icon-add icon-Solid"></i></a>
              </div>
            <% end %>
          </div>
          </td>
        <% end %>
      </tr></table>
      <div style="<%= hidden unless rubric && rubric.free_form_criterion_comments %> font-style: italic; font-size: 0.8em; margin: 5px;" class="custom_ratings">
        <%= t 'description.custom_comments_area', "This area will be used by the assessor to leave
        comments related to this criterion." %>
      </div>
    <% end %>
  </td>
  <td class="nobr points_form">
    <div class="editing" style="white-space: normal">
      <span style="white-space: nowrap; font-size: 0.8em">
        <%
          if assessing
            points_value = (n(assessment_rating.points) rescue "")
          else
            points_value = n((criterion.points rescue 5))
          end

          rating_points = %{
            <input
              type="text"
              aria-label="#{h(t("Points"))}"
              value="#{points_value}"
              class="criterion_points span1 no-margin-bottom"
            />
          }.html_safe
        %>
        <% if assessing && criterion && criterion.points %>
          <%= t "%{points} / %{total} pts", points: rating_points, total: n(criterion.points) %>
        <% else %>
          <%= t :points, "%{points} pts", :points => rating_points %>
        <% end %>
      </span><br />
    </div>
    <div class="displaying">
      <span style="white-space: nowrap;">
        <span class="criterion_rating_points_holder" style="<%= hidden unless assessment_rating %>">
          <span class="criterion_rating_points"><%= assessment_rating ? n(round_if_whole(assessment_rating[:points])) : nbsp %></span> /
        </span>
        <%= t :points, "%{points} pts", :points => %{<span class="display_criterion_points">#{ n((criterion.points rescue 5)) }</span>}.html_safe %><br />
      </span>
    </div>
    <div class="ignoring">
      <span> -- </span>
    </div>
    <div class="criterion_comments">
      <% if !rubric || !rubric.free_form_criterion_comments %>
        <a href="#" class="no-hover criterion_comments_link" title="<%= t 'links.additional_comments', "Additional Comments" %>">
          <%= image_tag "rubric_comment.png", :alt => t('icons.additional_comments', 'Additional Comments') %>
        </a>
        <div class="custom_rating" style="display: none;"><%= assessment_rating ? assessment_rating[:comments] : '' %></div>
      <% end %>
    </div>
  </td>
</tr>
